﻿using Microsoft.EntityFrameworkCore;
using TravelServer.Models;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using TravelServer.DTOs;

namespace TravelServer.DBContexs
{
    public class APPDBContex : IdentityDbContext<AppUser>
    {
        public APPDBContex(DbContextOptions<APPDBContex> options) : base(options)
        {

        }

        public DbSet<TouristRoute> TouristRoutes { get; set; }

        public DbSet<TouristRoutePictures> TouristRoutePictures { get; set; }
        public DbSet<ShoppingCart> ShoppingCarts { get; set; }
        public DbSet<LineItem> LineItems { get; set; }

        public DbSet<Order> Orders { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //通过json文件创建旅游路线默认数据
            //获取当前项目的根目录
            var basePath = AppDomain.CurrentDomain.BaseDirectory;
            var touristRoutePictureJson = File.ReadAllText(basePath + @"/DBContexs/touristRoutePicturesMockData.json");
            var touristRoutePictures = JsonConvert.DeserializeObject<List<TouristRoutePictures>>(touristRoutePictureJson);
            modelBuilder.Entity<TouristRoutePictures>().HasData(touristRoutePictures ?? new List<TouristRoutePictures>());

            var touristRouteJson = File.ReadAllText(basePath + @"/DBContexs/touristRoutesMockData.json");
            var touristRoutes = JsonConvert.DeserializeObject<List<TouristRoute>>(touristRouteJson);
            modelBuilder.Entity<TouristRoute>().HasData(touristRoutes ?? new List<TouristRoute>());

            //初始化用户与角色种子数据
            //1更新用户与角色外键Fluent写法
            modelBuilder.Entity<AppUser>(
                u => u.HasMany(x => x.UserRoles)
                .WithOne()
                .HasForeignKey(x => x.UserId)
                .IsRequired()
                );

            //2添加管理员角色  HasData 就是创建并存储到数据库中的过程方法
            var adminUserId = "user-dsfd33-fdsf1241-fdsf-fdsf-fdsfdsfds2";
            var adminRoleId = "role-dsfd33-fdsf1241-fdsf-fdsf-fdsfdsfds2";
            modelBuilder.Entity<IdentityRole>().HasData(

                new IdentityRole()
                {
                    Id = adminRoleId,
                    Name = "Admin",
                    NormalizedName = "Admin".ToUpper()
                }
                );

            //添加Admin用户
           
            AppUser appUser = new AppUser()
            {

                Id = adminUserId,
                UserName = "jhy",
                NormalizedUserName = "jhy".ToUpper(),
                Email = "631638489@qq.com",
                NormalizedEmail = "631638489@qq.com".ToUpper(),
                TwoFactorEnabled = false,
                EmailConfirmed = true,
                PhoneNumber = "15807162398",
                PhoneNumberConfirmed = true,
            };

            //构建passhash工具
            var ph = new PasswordHasher<AppUser>();
            //密码赋值
            appUser.PasswordHash = ph.HashPassword(appUser, "Jhy123!@#");
            modelBuilder.Entity<AppUser>() .HasData( appUser );


            //4 给用户加入管理员角色  关联上面创建的2个对象的ID，并存储到数据库中
            modelBuilder.Entity<IdentityUserRole<string>>().HasData(
                new IdentityUserRole<string>()
                {
                    RoleId = adminRoleId,
                    UserId = adminUserId,
                }

                );

           
        }

    }
}
